<?php
/**
 * 选品入库
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Network;

use App\Admin\Actions\NetworkGoods\OptionWarehousing;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\NetworkGoodsModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;


class GoodsController extends Controller
{
    use ModelForm;

    protected $header = '选品入库';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }


    public function grid(){

        $grid = new Grid(new GoodsModel());

        $grid->model()->where(['status'=>1])->orderBy('created_at', 'DESC');

        $grid->column('id','商品编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_title','商品标题')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_cover', '封面')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('goods_type','类型')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : GoodsTypeModel::selectValue('id',$filed);
        });

        $grid->column('goods_specs','规格')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('retail_goods_price','上货价')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('goods_price','零售价')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('goods_stock','库存')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('goods_sales','销量')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('status','状态')->display(function($filed){
            $data = NetworkGoodsModel::where(['network_id'=>admin::user()->id,'goods_id'=>$this->id])->select('id')->first();
            if(!empty($data)){
                return '<span class="label-info" style="width: 8px;height: 8px;padding: 0;border-radius: 50%;display: inline-block;"></span>&nbsp;&nbsp;已入库';
            }else{
                return '<span class="label-danger" style="width: 8px;height: 8px;padding: 0;border-radius: 50%;display: inline-block;"></span>&nbsp;&nbsp;未入库';
            }
        });

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('goods_name','商品名称');
                $filter->like('goods_specs','商品规格');
                $filter->between('created_at', '创建时间')->datetime();

            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
                $filter->equal('goods_type', '类型')->select(GoodsTypeModel::selectOptions());
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->disableActions();

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->append(new OptionWarehousing());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }


}
